package mdocejb.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entityBeans.ContactGroup;

import sessionBeans.Interfaces.IGestContactGroupRemote;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB(name = "GestContactGroupEntity")
	private IGestContactGroupRemote GCGR;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		if (request.getParameter("name").equals(
				request.getParameter("password"))) {
			response.sendRedirect("LoadContacts");
		} else {
			response.sendRedirect("index.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void init() {
		if (GCGR.findAll().size() == 0) {

			ContactGroup cg1 = new ContactGroup("Friends");
			ContactGroup cg2 = new ContactGroup("Colleague");
			ContactGroup cg3 = new ContactGroup("Acquaintance");
			ContactGroup cg4 = new ContactGroup("Family");

			GCGR.create(cg1);
			GCGR.create(cg2);
			GCGR.create(cg3);
			GCGR.create(cg4);
		}
		try {
			super.init();
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
